package cas.ihep.spark.test;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class SingleTestLauncher {
    @SuppressWarnings("unchecked")
    private static final List<String> run_list[]=new List[]{
            Arrays.asList("ssh", "hadoop01.ihep.ac.cn", "su - weizc /opt/spark.test.sh", "/workfs/cc/weizc/alluxio-test/01/oop01.lua", "/workfs/cc/weizc/alluxio-test/trigger.lua"),
            Arrays.asList("ssh", "hadoop02.ihep.ac.cn", "su - weizc /opt/spark.test.sh", "/workfs/cc/weizc/alluxio-test/02/oop01.lua", "/workfs/cc/weizc/alluxio-test/trigger.lua"),
            Arrays.asList("ssh", "hadoop03.ihep.ac.cn", "su - weizc /opt/spark.test.sh", "/workfs/cc/weizc/alluxio-test/03/oop01.lua", "/workfs/cc/weizc/alluxio-test/trigger.lua"),
            Arrays.asList("ssh", "hadoop04.ihep.ac.cn", "su - weizc /opt/spark.test.sh", "/workfs/cc/weizc/alluxio-test/04/oop01.lua", "/workfs/cc/weizc/alluxio-test/trigger.lua"),
            Arrays.asList("ssh", "hadoop01.ihep.ac.cn", "su - weizc /opt/spark.test.sh", "/workfs/cc/weizc/alluxio-test/01/oop02.lua", "/workfs/cc/weizc/alluxio-test/trigger.lua"),
            Arrays.asList("ssh", "hadoop02.ihep.ac.cn", "su - weizc /opt/spark.test.sh", "/workfs/cc/weizc/alluxio-test/02/oop02.lua", "/workfs/cc/weizc/alluxio-test/trigger.lua"),
            Arrays.asList("ssh", "hadoop03.ihep.ac.cn", "su - weizc /opt/spark.test.sh", "/workfs/cc/weizc/alluxio-test/03/oop02.lua", "/workfs/cc/weizc/alluxio-test/trigger.lua"),
            Arrays.asList("ssh", "hadoop04.ihep.ac.cn", "su - weizc /opt/spark.test.sh", "/workfs/cc/weizc/alluxio-test/04/oop02.lua", "/workfs/cc/weizc/alluxio-test/trigger.lua"),
            Arrays.asList("ssh", "hadoop01.ihep.ac.cn", "su - weizc /opt/spark.test.sh", "/workfs/cc/weizc/alluxio-test/01/oop03.lua", "/workfs/cc/weizc/alluxio-test/trigger.lua"),
            Arrays.asList("ssh", "hadoop02.ihep.ac.cn", "su - weizc /opt/spark.test.sh", "/workfs/cc/weizc/alluxio-test/02/oop03.lua", "/workfs/cc/weizc/alluxio-test/trigger.lua"),
            Arrays.asList("ssh", "hadoop03.ihep.ac.cn", "su - weizc /opt/spark.test.sh", "/workfs/cc/weizc/alluxio-test/03/oop03.lua", "/workfs/cc/weizc/alluxio-test/trigger.lua"),
            Arrays.asList("ssh", "hadoop04.ihep.ac.cn", "su - weizc /opt/spark.test.sh", "/workfs/cc/weizc/alluxio-test/04/oop03.lua", "/workfs/cc/weizc/alluxio-test/trigger.lua"),
            Arrays.asList("ssh", "hadoop01.ihep.ac.cn", "su - weizc /opt/spark.test.sh", "/workfs/cc/weizc/alluxio-test/01/oop04.lua", "/workfs/cc/weizc/alluxio-test/trigger.lua"),
            Arrays.asList("ssh", "hadoop02.ihep.ac.cn", "su - weizc /opt/spark.test.sh", "/workfs/cc/weizc/alluxio-test/02/oop04.lua", "/workfs/cc/weizc/alluxio-test/trigger.lua"),
            Arrays.asList("ssh", "hadoop03.ihep.ac.cn", "su - weizc /opt/spark.test.sh", "/workfs/cc/weizc/alluxio-test/03/oop04.lua", "/workfs/cc/weizc/alluxio-test/trigger.lua"),
            Arrays.asList("ssh", "hadoop04.ihep.ac.cn", "su - weizc /opt/spark.test.sh", "/workfs/cc/weizc/alluxio-test/04/oop04.lua", "/workfs/cc/weizc/alluxio-test/trigger.lua"),
            Arrays.asList("ssh", "hadoop01.ihep.ac.cn", "su - weizc /opt/spark.test.sh", "/workfs/cc/weizc/alluxio-test/01/oop05.lua", "/workfs/cc/weizc/alluxio-test/trigger.lua"),
            Arrays.asList("ssh", "hadoop02.ihep.ac.cn", "su - weizc /opt/spark.test.sh", "/workfs/cc/weizc/alluxio-test/02/oop05.lua", "/workfs/cc/weizc/alluxio-test/trigger.lua"),
            Arrays.asList("ssh", "hadoop03.ihep.ac.cn", "su - weizc /opt/spark.test.sh", "/workfs/cc/weizc/alluxio-test/03/oop05.lua", "/workfs/cc/weizc/alluxio-test/trigger.lua"),
            Arrays.asList("ssh", "hadoop04.ihep.ac.cn", "su - weizc /opt/spark.test.sh", "/workfs/cc/weizc/alluxio-test/04/oop05.lua", "/workfs/cc/weizc/alluxio-test/trigger.lua"),
            Arrays.asList("ssh", "hadoop01.ihep.ac.cn", "su - weizc /opt/spark.test.sh", "/workfs/cc/weizc/alluxio-test/01/oop06.lua", "/workfs/cc/weizc/alluxio-test/trigger.lua"),
            Arrays.asList("ssh", "hadoop02.ihep.ac.cn", "su - weizc /opt/spark.test.sh", "/workfs/cc/weizc/alluxio-test/02/oop06.lua", "/workfs/cc/weizc/alluxio-test/trigger.lua"),
            Arrays.asList("ssh", "hadoop03.ihep.ac.cn", "su - weizc /opt/spark.test.sh", "/workfs/cc/weizc/alluxio-test/03/oop06.lua", "/workfs/cc/weizc/alluxio-test/trigger.lua"),
            Arrays.asList("ssh", "hadoop04.ihep.ac.cn", "su - weizc /opt/spark.test.sh", "/workfs/cc/weizc/alluxio-test/04/oop06.lua", "/workfs/cc/weizc/alluxio-test/trigger.lua"),
            Arrays.asList("ssh", "hadoop01.ihep.ac.cn", "su - weizc /opt/spark.test.sh", "/workfs/cc/weizc/alluxio-test/01/oop07.lua", "/workfs/cc/weizc/alluxio-test/trigger.lua"),
            Arrays.asList("ssh", "hadoop02.ihep.ac.cn", "su - weizc /opt/spark.test.sh", "/workfs/cc/weizc/alluxio-test/02/oop07.lua", "/workfs/cc/weizc/alluxio-test/trigger.lua"),
            Arrays.asList("ssh", "hadoop03.ihep.ac.cn", "su - weizc /opt/spark.test.sh", "/workfs/cc/weizc/alluxio-test/03/oop07.lua", "/workfs/cc/weizc/alluxio-test/trigger.lua"),
            Arrays.asList("ssh", "hadoop04.ihep.ac.cn", "su - weizc /opt/spark.test.sh", "/workfs/cc/weizc/alluxio-test/04/oop07.lua", "/workfs/cc/weizc/alluxio-test/trigger.lua"),
            Arrays.asList("ssh", "hadoop01.ihep.ac.cn", "su - weizc /opt/spark.test.sh", "/workfs/cc/weizc/alluxio-test/01/oop08.lua", "/workfs/cc/weizc/alluxio-test/trigger.lua"),
            Arrays.asList("ssh", "hadoop02.ihep.ac.cn", "su - weizc /opt/spark.test.sh", "/workfs/cc/weizc/alluxio-test/02/oop08.lua", "/workfs/cc/weizc/alluxio-test/trigger.lua"),
            Arrays.asList("ssh", "hadoop03.ihep.ac.cn", "su - weizc /opt/spark.test.sh", "/workfs/cc/weizc/alluxio-test/03/oop08.lua", "/workfs/cc/weizc/alluxio-test/trigger.lua"),
            Arrays.asList("ssh", "hadoop04.ihep.ac.cn", "su - weizc /opt/spark.test.sh", "/workfs/cc/weizc/alluxio-test/04/oop08.lua", "/workfs/cc/weizc/alluxio-test/trigger.lua"),
    };

    public static void main(String args[]){
        int count=Integer.parseInt(args[0]);
        ExecutorService pool= Executors.newFixedThreadPool(count);
        for(int i=0;i<count && i<run_list.length;i++){
            final int j=i;
            pool.submit(new Runnable() {
                @Override
                public void run() {
                    ProcessBuilder builder=new ProcessBuilder(run_list[j]);
                    builder.redirectOutput(new File("hadoop.out."+j));
                    builder.redirectError(new File("hadoop.err."+j));
                    try {
                        builder.start().waitFor();
                    } catch (InterruptedException | IOException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
        pool.shutdown();
        try {
            pool.awaitTermination(1000, TimeUnit.HOURS);
        } catch (InterruptedException ignored) {
        }
    }
}
